package io.persimmon.core.exception;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Objects;

public class PersimmonExceptionFactory {
    public static void assertIf(boolean isCreate, String message) {
        if (isCreate) {
            throw create(message);
        }
    }

    public static void assertNull(Object obj, String message) {
        if (obj == null) {
            throw create(message);
        }
    }

    public static void assertEmpty(Object obj, String message) {
        if (Objects.isNull(obj)) {
            throw create(message);
        }
        if (obj instanceof String) {
            if (StringUtils.isEmpty((CharSequence) obj)) {
                throw create(message);
            }
        } else if (obj instanceof Collection) {
            if (CollectionUtils.isEmpty((Collection<?>) obj)) {
                throw create(message);
            }
        }
    }

    public static PersimmonException create(Throwable t) {
        return new PersimmonException(t);
    }

    public static PersimmonException create(String message) {
        return new PersimmonException(message);
    }
}
